package com.lsh.payment.core.util.pay.icp;

import cfca.util.Base64;
import cfca.x509.certificate.X509Cert;
import com.lsh.payment.core.strategy.config.IpcPayConfig;

import java.io.FileInputStream;

/**
 * Project Name: lsh-payment
 * Created by peter on 17/11/20.
 * 北京链商电子商务有限公司
 * Package
 * desc:
 */
public class RsaPayUtil {

    public static String getPlatinText(String ciphertext) throws Exception {

        RsaP1Util rsaP1Util = new RsaP1Util();
        String platinText = new String(rsaP1Util.openMessage(ciphertext, IpcPayConfig.merchantPfxPath, "umbpay"));

        return platinText;
    }


    public static String getCert() throws Exception {

        FileInputStream is = new FileInputStream(IpcPayConfig.publicKeyPath);
        X509Cert x509Cert = new X509Cert(is);
        String cert = new String(Base64.encode(x509Cert.getEncoded()), "UTF-8");

        return cert;
    }


    public static String getSign(String body) throws Exception {

        RsaP1Util rsaP1Util = new RsaP1Util();
        String sign = rsaP1Util.rsaP1Sign(body, IpcPayConfig.merchantPfxPath, "umbpay");

        return sign;
    }


    public static String envelopText(String data) throws Exception {

        RsaP1Util rsaP1Util = new RsaP1Util();
        String reqText = new String(rsaP1Util.envelopMessage(data, IpcPayConfig.publicKeyPath));

        return reqText;
    }


    public static boolean verify(String returnSign,String buildSign) throws Exception {

        RsaP1Util rsaP1Util = new RsaP1Util();
        String cert = RsaPayUtil.getCert();

        return rsaP1Util.rsaP1Verify(returnSign, buildSign, cert.getBytes("UTF-8"));
    }
}
